package com.airticket.demo.controller;

import com.airticket.demo.bean.AirlinePer;
import com.airticket.demo.bean.Airport_7days;
import com.airticket.demo.bean.EachTimeFlow;
import com.airticket.demo.bean.JsonResult;
import com.airticket.demo.bean.result.AirCompany;
import com.airticket.demo.bean.result.AirCompanyResult;
import com.airticket.demo.bean.result.AirlinesEachTime;
import com.airticket.demo.bean.result.SevenLatestLines;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 左视图渲染数据接口
 */
@RestController
public class LeftViewController {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 成都近七日航班量
     * @return
     */
    @GetMapping("/bar/sevenLatestLines")
    public JsonResult<SevenLatestLines> getsevenLatestLines()
    {
        //返回数据
        JsonResult<SevenLatestLines> jsonResult;
        //设定查询条件
        Query query=new Query();
        //按时间进行降序排列
        Sort sort=Sort.by(Sort.Direction.DESC,"flightDate");
        //查询对应数据集合
        List<Airport_7days> list=mongoTemplate.find(query.with(sort).limit(7),Airport_7days.class);
        //查询结果验证
        if(list!=null && list.size()>0)
        {
            List<String> dates=new ArrayList<>();
            List lines=new ArrayList();
            int size=list.size();
            for(int i=size-1;i>=0;i--)
            {
                dates.add(list.get(i).getFlightDate().substring(5,10));
                lines.add(list.get(i).getCount());
            }
            SevenLatestLines sevenLatestLines=new SevenLatestLines(dates,lines);
            //返回数据封装
            jsonResult=new JsonResult<>(true,"查询成功",sevenLatestLines);
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }

    /**
     *各时段起飞数量
     * @return
     */
    @GetMapping("/line/takeOffEachTime")
    public JsonResult<AirlinesEachTime> getAirlinesEachTime()
    {
        JsonResult<AirlinesEachTime> jsonResult;
        //设置查询条件，按每天的小时进行升序排列
        Sort sort=Sort.by(Sort.Direction.ASC,"ShowTime");
        Query query=new Query();
        query.with(sort);
        query.fields().include("dep_count");
        //查询机场航站楼集合
        List<EachTimeFlow> A=mongoTemplate.find(query, EachTimeFlow.class,"T1_EachTime_Flow");
        List<EachTimeFlow> B=mongoTemplate.find(query, EachTimeFlow.class,"T2_EachTime_Flow");
        //查询结果验证
        if(A!=null && A.size()>0 && B!=null && B.size()>0)
        {
            List takeOffSum=new ArrayList();
            //每两个小时进行数据的合并
            for(int i=0;i<24;i=i+2)
            {
                int takeOffSumA=A.get(i).getDep_count()+A.get(i+1).getDep_count();
                int takeOffSumB=B.get(i).getDep_count()+B.get(i+1).getDep_count();
                takeOffSum.add(takeOffSumA+takeOffSumB);
            }
            //返回数据封装
            jsonResult=new JsonResult<>(true,"查询成功",new AirlinesEachTime(takeOffSum));

        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }

    /**
     * 航司占比
     * @return
     */
    @GetMapping("/pie/companyRate")
    public JsonResult<AirCompanyResult> getCompanyRate()
    {
        //返回数据
        JsonResult jsonResult;
        //设定查询条件
        Query query=new Query();
        //对各航空公司按航班数进行降序排列，并去前五的数据
        Sort sort=Sort.by(Sort.Direction.DESC,"count");
        query.with(sort).limit(5);
        //投影指定字段
        query.fields().include("airlineName").include("count");
        //查询航班量大于0的数据
        Criteria criteria=Criteria.where("count").gt(0);
        query.addCriteria(criteria);
        //数据库查询
        List<AirlinePer> list=mongoTemplate.find(query,AirlinePer.class);
        if(list!=null && list.size()>0)
        {
            List<AirCompany> airCompanies=new ArrayList<>();
            for(AirlinePer airlinePer:list)
            {
                //取countRate字段后两位并将其转为int
                int rate=airlinePer.getCount();
                AirCompany airCompany=new AirCompany(airlinePer.getAirlineName(),rate);
                airCompanies.add(airCompany);
            }
            //返回数据封装
            jsonResult=new JsonResult(true,"查询成功",new AirCompanyResult(airCompanies));
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }

}
